package br.mg.bhe.ftpobre.fs;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

/**
 * 
 * @author Aureliano
 */
public class OpenWindowsFile implements IOpenFile {

	@Override
	public void open(File file) {
		String os = System.getProperty("os.name") + " " + System.getProperty("os.version");
		os = os.toLowerCase();
		if (os.contains("xp") || os.contains("2003")) {
			this.openForWindowXpOr2003(file);
		} else {
			this.openWithNativeApi(file);
		}
	}
	
	private void openWithNativeApi(File file) {
		try {
			Desktop.getDesktop().open(file);
		} catch (IOException ex) {
			throw new RuntimeException(ex);
		}
	}
	
	private void openForWindowXpOr2003(File file) {
		try {
			String cmd = "rundll32 url.dll,FileProtocolHandler " + file.getCanonicalPath();
			Runtime.getRuntime().exec(cmd);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}	
	}
}